<?php
namespace app\common\service;


use app\common\model\ProductOrder;
use app\web\constant\CommonConstant;
use think\Db;

/**
 * 房费相关服务
 *
 * Class RoomCostService
 * @package app\common\service
 */
class RoomCostService {

    /**
     * 查询房费
     */
    public static function selectRoomCost($store_id,$room_type){

        $room_data = Db::name('qt_room_price')
            ->where('store_id', $store_id)
            ->where('room_type', $room_type)
            ->find();

        return $room_data;
    }


    /**
     * 结账根据房间类类型，查询商品ID，并写入单据
     */
    public static function saveCostOrder($store_id,$room_id,$user_info,$hand_card_id='') {
        $room_data = Db::name('qt_room')
            ->where('room_cost_start_time','>',0)
            ->where('id',$room_id)
            ->find();
        if (empty($room_data)) {
            return false;
        }

        $room_cost = RoomCostService::selectRoomCost($store_id,$room_data['type_id']);
        if (!empty($room_cost)) {

            //[{"product_id":18,"num":1,"product_name":"果粒橙","product_price":"4.00","goods_total_price":4}]
            $product_data = Db::name('qt_goods')->where('id',$room_cost['product_id'])->find();
            if (!empty($room_cost['overtime'])) {

                //根据房间计时时间，当前时间，判断计算需要插入几条商品数据
                $diff_time = (time() - $room_data['room_cost_start_time'])/60;

                $free_time = $room_cost['free_time'] + $room_cost['buffer_time'];//免费时间+缓冲时间
                if ($diff_time < $free_time) {//免费时间
                    return false;//小于免费时间
                }
                //overtime '超时时间：每隔多少时间 0 无限超时',
                $num = ceil(($diff_time - $free_time) / $room_cost['overtime']);//向上取整
                if ($num < 10 && $num > $room_data['room_cost_num']) {//写十次测试环境超时球了

                    for ($i=1;$i<=$num-$room_data['room_cost_num'];$i++) {

                        //插入一条
                        $data = [
                            'hand_card_id'=>$hand_card_id,
                            'room_id'=>$room_data['id'],
                            'engineer_id'=>'',
                            'total_price'=>!empty($product_data['goods_total_price']) ? $product_data['goods_total_price'] : 0,
                            'goods'=>json_encode([
                                [
                                    'product_id'=>$product_data['id'],
                                    'num'=>1,
                                    'product_name'=>$product_data['name'],
                                    'product_price'=>$product_data['price'],
                                    'goods_total_price'=>$product_data['price'],
                                    'business_type'=>2,
                                ]
                            ]),
                            'content'=>'',
                        ];

                        (new ProductOrder())->createOrder($data,$user_info);
                        //更新收取房费次数
                        Db::name('qt_room')->where('id',$room_data['id'])->inc('room_cost_num')->update();
                    }
                }
            }else {
                //备注：间隔时间填0或者不填代表只收一次房费
                if ($room_data['room_cost_num'] >=1) {
                    return false;
                }

                //插入一条
                $data = [
                    'hand_card_id'=>$hand_card_id,
                    'room_id'=>$room_data['id'],
                    'engineer_id'=>'',
                    'total_price'=>!empty($product_data[0]['goods_total_price']) ? $product_data[0]['goods_total_price'] : 0,
                    'goods'=>json_encode([
                        [
                            'product_id'=>$product_data['id'],
                            'num'=>1,
                            'product_name'=>$product_data['name'],
                            'product_price'=>$product_data['price'],
                            'goods_total_price'=>$product_data['price'],
                            'business_type'=>2,
                        ]
                    ]),
                    'content'=>'',
                ];
                (new ProductOrder())->createOrder($data,$user_info);

                //更新收取房费次数
                Db::name('qt_room')->where('id',$room_data['id'])->inc('room_cost_num')->update();
            }
        }

    }

    //修改房间单据列表页面，商品区分是否是房费字段


    //修改结账，区分商品是否是房费


    //修改查询的口，区分是否是房费



}